package com.bage.common.service.impl;

import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.bage.common.exception.BizException;
import com.bage.common.service.SmsCommonService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Slf4j
@Service
@RequiredArgsConstructor
@ConditionalOnBean(AsyncClient.class)
public class AliSmsServiceImpl implements SmsCommonService {

    final AsyncClient asyncClient;

    final ObjectMapper objectMapper;

    @Override
    public void sendSms(String signName, String templateCode, String phoneNumbers, Map<String, String> templateParam) {
        try {
            SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                    .signName(signName)
                    .templateCode(templateCode)
                    .phoneNumbers(phoneNumbers)
                    .templateParam(objectMapper.writeValueAsString(templateParam))
                    .build();
            CompletableFuture<SendSmsResponse> response = asyncClient.sendSms(sendSmsRequest);
            SendSmsResponse resp = response.get();
            log.info("短信发送结果: {}", objectMapper.writeValueAsString(resp));
        } catch (Exception e) {
            throw new BizException("发送短信验证码失败", e);
        }
    }
}
